package com.cheer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cheer.common.Result;
import com.cheer.param.ActivityAchieveUpParam;
import com.cheer.param.ActivityAddOrUpParam;
import com.cheer.param.ActivityPageParam;
import com.cheer.service.OaActivityService;
import com.cheer.vo.ActivityAchieveVO;
import com.cheer.vo.ActivityInfoVO;
import com.cheer.vo.ActivityPageVO;
import com.cheer.vo.ImgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author cfz
 * 用户活动 controller
 */
@RestController
@Api(tags = "活动 TODO")
@RequestMapping("/activity")
public class OaActivityController {

    @Autowired
    private OaActivityService oaActivityService;

    @ApiOperation(value = "添加活动 √ TODO",notes = "机构关系 oa/learn/class/sub?id=0")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid ActivityAddOrUpParam activityAddOrUpParam) {
        oaActivityService.add(activityAddOrUpParam);
        return Result.success();
    }

    @ApiOperation("活动列表 √ TODO")
    @PostMapping("/page")
    @PreAuthorize("@permissionValidator.isAdminOrOtherAdmin()")
    public Result<IPage<ActivityPageVO>> list( @RequestBody ActivityPageParam activityPageParam) {
        IPage<ActivityPageVO> page = oaActivityService.page(activityPageParam);
        return Result.success(page);
    }

    @ApiOperation(value = "活动回显 √ TODO", notes = "机构回显调用 /oa/learn/class/info?id={pid}")
    @PostMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<ActivityInfoVO> info(Integer id) {
        ActivityInfoVO activityInfoVO = oaActivityService.info(id);
        return Result.success(activityInfoVO);
    }

    @ApiOperation("修改活动 TODO")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid ActivityAddOrUpParam activityAddOrUpParam) {
        oaActivityService.update(activityAddOrUpParam);
        return Result.success();
    }

    @ApiOperation("图片上传(单) √ TODO")
    @PostMapping("/upImage")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<ImgVO> upImage(@RequestPart("file") MultipartFile file) {
        ImgVO imgVO = oaActivityService.uploadImage(file);
        return Result.success(imgVO);
    }

    @ApiOperation("图片上传(多) √ TODO")
    @PostMapping("/upImages")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<List<ImgVO>> upImages(@RequestPart("file") MultipartFile[] file) {
        List<ImgVO> list = oaActivityService.upImages(file);
        return Result.success(list);
    }

    @ApiOperation("录入活动信息(回显) √ TODO")
    @GetMapping("/achievementInfo")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result achievementInfo(Integer id) {
        ActivityAchieveVO achieveVO = oaActivityService.achievementInfo(id);
        return Result.success(achieveVO);
    }

    @ApiOperation("录入活动信息(修改) √ TODO")
    @PostMapping("/achievementUpdate")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result achievementUpdate(@RequestBody @Valid ActivityAchieveUpParam activityAchieveUpParam) {
        oaActivityService.achievementUpdate(activityAchieveUpParam);
        return Result.success();
    }

    @ApiOperation("发布或者取消发布 √ TODO")
    @GetMapping("/pubOrCancel")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result pubOrCancel(Integer id) {
        oaActivityService.pubOrCancel(id);
        return Result.success();
    }

    @ApiOperation("删除 TODO")
    @GetMapping("/del")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result del(Integer id) {
        oaActivityService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "导入成员 √ TODO", notes = "模板文件下载地址: /oa/活动人员导入模板.xlsx")
    @PostMapping("/toLead")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result toLead(@RequestPart("file") MultipartFile file, Integer id) throws IOException {
        oaActivityService.toLead(file, id);
        return Result.success();
    }

}
